const baseController = require('./base.js');
const yapi = require('../yapi.js');
const userModel = require('../models/user.js');
const dubboMethodModel = require('../models/dubboMethod.js');
// const InterfaceModel = require('../models/dubboInterface.js');
const jsondiffpatch = require('jsondiffpatch');
const formattersHtml = jsondiffpatch.formatters.html;
const showDiffMsg = require('../../common/diff-view.js');
const dubboInterfaceModel = require('../models/dubboInterface.js');
const dubboProjectModel = require('../models/dubboProject.js');
const dubboCaseModel = require('../models/dubboCase.js');
// const _ = require('underscore');
const Mock = require('mockjs');
const Random = Mock.Random;

class dubboMethodController extends baseController {
  constructor(ctx) {
    super(ctx);
    this.Model = yapi.getInst(dubboMethodModel);
    this.userModel = yapi.getInst(userModel);
    this.dubbopProjectModel = yapi.getInst(dubboProjectModel);
    this.interfaceModel = yapi.getInst(dubboInterfaceModel);
    this.caseModel = yapi.getInst(dubboCaseModel);
  }

  // 添加方法
  async add(ctx) {
    let params = ctx.params;

    if (!this.$tokenAuth) {
      let auth = await this.checkAuth(params.projectid, 'dubboproject', 'edit');

      if (!auth) {
        return (ctx.body = yapi.commons.resReturn(null, 40033, '没有权限'));
      }
    }
    let checkRepeat = await this.Model.checkRepeat(params.infid, params.mname);

    if (checkRepeat > 0) {
      return (ctx.body = yapi.commons.resReturn(
          null,
          40022,
          '已存在的方法:' + params.mname
      ));
    }

    let inf_data = this.interfaceModel.get(params.infid);

    let data = Object.assign(params, {
      projectid: inf_data.projectid,
      infid: params.infid,
      cname:params.cname,
      mname: params.mname,
      inname: params.inname,
      outname: params.outname,
      status:params.status,
      ischl:params.ischl,
      cuserid: this.getUid(),
      euserid: this.getUid(),
      ctime: yapi.commons.time(),
      etime: yapi.commons.time()
    });

    let uid = this.getUid();

    if (this.getRole() !== 'admin' && uid !== 999999) {
      let userdata = await yapi.commons.getUserdata(uid, 'dev');
      // 检查一下是否有这个人
      let check = await this.dubbopProjectModel.checkMemberRepeat(params.projectid, uid);
      if (check === 0 && userdata) {
        await this.dubbopProjectModel.addMember(params.projectid, [userdata]);
      }
    }

    let result = await this.Model.save(data);

    this.interfaceModel.get(params.infid).then(inf => {
      let username = this.getUsername();
      let title = `<a href="/user/profile/${this.getUid()}">${username}</a> 为接口 <a href="/dubboproject/${
          params.projectid
      }/interface/api/${params.infid}">${inf.cname}</a> 添加了方法 <a href="/dubboproject/${
          params.projectid
      }/interface/dubbomethod/${result._id}">${data.mname}</a>`;

      yapi.commons.saveLog({
        content: title,
        type: 'dubboproject',
        uid: this.getUid(),
        username: username,
        typeid: params.projectid
      });
      this.dubboProjectModel.up(params.projectid, {up_time: new Date().getTime()}).then();
    });

    ctx.body = yapi.commons.resReturn(result);
  }

  /**
   * 查找接口方法
   * @interface /dubbomethod/get
   * @method GET
   * @category interface
   * @foldnumber 10
   * @param {Number}   id 接口方法id,不能为空
   * @returns {Object}
   * @example
   */
  async get(ctx) {
    let params = ctx.params;
    if (!params.id) {
      return (ctx.body = yapi.commons.resReturn(null, 400, '接口方法id不能为空'));
    }

    try {
      let ret = await this.Model.get(params.id);
      if (!this.$tokenAuth) {
        const parentInterface = await this.interfaceModel.get(ret.infid)
        let auth = await this.checkAuth(parentInterface.projectid, 'dubboproject', 'view');
  
        if (!auth) {
          return (ctx.body = yapi.commons.resReturn(null, 40033, '没有权限'));
        }
      }
      let newRet = {
        cname: ret.cname,
        mname: ret.mname,
        inname: ret.inname,
        inbody: ret.inbody,
        outname: ret.outname,
        outbody: ret.outbody,
        ctime: ret.ctime,
        cuserid: ret.cuserid,
        etime: ret.etime,
        euserid: ret.euserid,
        status: ret.status,
        onlinedate: ret.onlinedate,
        batch: ret.batch,
        infid: ret.infid,
        ischl: ret.ischl,
        chltype: ret.chltype,
        isapi: ret.isapi,
        url: ret.url,
        projectid: ret.projectid,
        desc: ret.desc,
        index: ret.index

      }
      let uesrR = undefined
      if (!(newRet.cuserid == undefined)) {
        uesrR= await this.userModel.findById(newRet.cuserid);
        newRet.cusername = uesrR.username;
      }
      if (!(newRet.euserid == undefined)) {
        uesrR = await this.userModel.findById(newRet.euserid);
        newRet.eusername = uesrR.username;
      }
      newRet._id = parseInt(params.id);

      ctx.body = yapi.commons.resReturn({
        curMethod: newRet
      });
    } catch (err) {
      ctx.body = yapi.commons.resReturn(null, 402, err.message);
    }
  }

  /**
   * 更新接口方法
   * @interface /dubbomethod/up
   * @method POST
   * @category interface
   * @foldnumber 10
   * @param {Number}   id 接口方法id,不能为空
   * @returns {Object}
   * @example
   */
  // TODO: 渠道号查重
  async up(ctx) {
    let params = ctx.params;
    try {
      // TODO: 添加验证
      if (!params.id) {
        return (ctx.body = yapi.commons.resReturn(null, 400, '接口方法id不能为空'));
      }
  
      
      let tarMethod = await this.Model.get(params.id);
      if (!tarMethod) {
        return (ctx.body = yapi.commons.resReturn(null, 400, '不存在的接口方法'));
      }
      
      if (!this.$tokenAuth) {
        const parentInterface = await this.interfaceModel.get(tarMethod.infid)
        let auth = await this.checkAuth(parentInterface.projectid, 'dubboproject', 'edit');
  
        if (!auth) {
          return (ctx.body = yapi.commons.resReturn(null, 40033, '没有权限'));
        }
      }

      if (!(params.chltype == undefined)) {
        if (!(params.chltype.length === 6)) {
          return (ctx.body = yapi.commons.resReturn(null, 400, '渠道号长度应为6'))
        }
        let listForCheck = await this.Model.listByIdAndChltype(params.id, params.chltype)
        if (listForCheck == undefined || listForCheck.length === 0) {
        } else {
          return (ctx.body = yapi.commons.resReturn(null, 400, '渠道号重复'));
        }
      }
      params.euserid = this.getUid()

      await this.Model.up(params.id, params);
      const oldDubboMethod = tarMethod;
      const newDubboMethod = await this.Model.get(params.id);
      const parentInterface = await this.interfaceModel.get(newDubboMethod.infid);
      
      let logData = {
        type: 'method_up',
        dubbo_method_id: params.id,
        dubbo_interface_id: newDubboMethod.infid,
        current: newDubboMethod,
        old: oldDubboMethod
      };

      let diffView2 = showDiffMsg(jsondiffpatch, formattersHtml, logData);
      if (diffView2 === false) {
        // 没有变化时，不写日志
      } else {
        yapi.commons.saveLog({
          content: `<a href="/user/profile/${this.getUid()}">${this.getUsername()}</a> 
                      更新了dubbo接口 <a href="/dubboproject/${parentInterface.projectid}/interface/api/cat_${
              newDubboMethod.infid
          }">${parentInterface.cname}</a> 
                      下的方法 <a href="/dubboproject/${parentInterface.projectid}/interface/api/${params.id}">${
              newDubboMethod.mname
          }</a>`,
          type: 'dubboproject',
          uid: this.getUid(),
          username: this.getUsername(),
          typeid: parentInterface.projectid,
          data: logData
        });
      }
      let ret = newDubboMethod

      let newRet = {
        cname: ret.cname,
        mname: ret.mname,
        inname: ret.inname,
        inbody: ret.inbody,
        outname: ret.outname,
        outbody: ret.outbody,
        ctime: ret.ctime,
        cuserid: ret.cuserid,
        etime: ret.etime,
        euserid: ret.euserid,
        status: ret.status,
        onlinedate: ret.onlinedate,
        batch: ret.batch,
        infid: ret.infid,
        ischl: ret.ischl,
        chltype: ret.chltype,
        isapi: ret.isapi,
        url: ret.url,
        projectid: ret.projectid,
        desc: ret.desc,
        index: ret.index

      }
      let uesrR = undefined
      if (!(newRet.cuserid == undefined)) {
        uesrR= await this.userModel.findById(newRet.cuserid);
        newRet.cusername = uesrR.username;
      }
      if (!(newRet.euserid == undefined)) {
        uesrR = await this.userModel.findById(newRet.euserid);
        newRet.eusername = uesrR.username;
      }
      newRet._id = parseInt(params.id);

      ctx.body = yapi.commons.resReturn(newRet);
    } catch (err) {
      ctx.body = yapi.commons.resReturn(null, 402, err.message);
    }
  }

      // 获取写入log所需数据
/**
   * 根据dubbo项目id获取所有方法
   * @interface /dubbomethod/listByMenu
   * @method GET
   * @category dubbomethod
   * @foldnumber 10
   * @param {Number}   projectid dubbo项目id,不能为空
   * @returns {Object}
   * @example
   */
  async listByMenu(ctx) {
    let projectid = ctx.params.projectid;
    if (!projectid) {
      return (ctx.body = yapi.commons.resReturn(null, 400, 'Dubbo项目id不能为空'));
    }
    let dubboProject = await this.dubboProjectModel.getBaseInfo(projectid);
    if (!dubboProject) {
      return (ctx.body = yapi.commons.resReturn(null, 406, '不存在的dubbo项目'));
    }
    if (dubboProject.projecttype === 'private') {
      if ((await this.checkAuth(dubboProject._id, 'dubboproject', 'view')) !== true) {
        return (ctx.body = yapi.commons.resReturn(null, 406, '没有权限'));
      }
    }

    try {
      let result = await this.interfaceModel.listByPid(projectid),
          newResult = [];
      for (let i = 0, item, list; i < result.length; i++) {
        item = result[i].toObject();
        list = await this.Model.listByInfId(item._id);
        for (let j = 0; j < list.length; j++) {
          list[j] = list[j].toObject();
        }

        item.list = list;
        newResult[i] = item;
      }
      ctx.body = yapi.commons.resReturn(newResult);
    } catch (err) {
      ctx.body = yapi.commons.resReturn(null, 402, err.message);
    }
  }

  // list
  async list(ctx) {
    let projectid = ctx.params.projectid;
    let page = ctx.request.query.page || 1,
        limit = ctx.request.query.limit || 10;
    let dubboProject = await this.dubboProjectModel.getBaseInfo(projectid);
    if (!dubboProject) {
      return (ctx.body = yapi.commons.resReturn(null, 407, '不存在的dubbo项目'));
    }
    if (dubboProject.projecttype === 'private') {
      if ((await this.checkAuth(dubboProject._id, 'dubboproject', 'view')) !== true) {
        return (ctx.body = yapi.commons.resReturn(null, 406, '没有权限'));
      }
    }
    if (!dubboProject) {
      return (ctx.body = yapi.commons.resReturn(null, 400, 'dubbo项目id不能为空'));
    }

    try {
      let result;
      if (limit === 'all') {
        result = await this.Model.list(projectid);
      } else {
        result = await this.Model.listWithPage(projectid, page, limit);
      }

      let count = await this.Model.listCount({ projectid });

      ctx.body = yapi.commons.resReturn({
        count: count,
        total: Math.ceil(count / limit),
        list: result
      });
      yapi.emitHook('method_list', result).then();
    } catch (err) {
      ctx.body = yapi.commons.resReturn(null, 402, err.message);
    }
  }
  async listexport(ctx) {
    let projectid = ctx.params.projectid;
    let page = ctx.request.query.page || 1,
        limit = ctx.request.query.limit || 10;
    let dubboProject = await this.dubboProjectModel.getBaseInfo(projectid);
    if (!dubboProject) {
      return (ctx.body = yapi.commons.resReturn(null, 407, '不存在的dubbo项目'));
    }
    if (dubboProject.projecttype === 'private') {
      if ((await this.checkAuth(dubboProject._id, 'dubboproject', 'view')) !== true) {
        return (ctx.body = yapi.commons.resReturn(null, 406, '没有权限'));
      }
    }
    if (!dubboProject) {
      return (ctx.body = yapi.commons.resReturn(null, 400, 'dubbo项目id不能为空'));
    }
  
    try {
      
      let infresult = await this.interfaceModel.list(projectid);
      
      //数组转换成map
      let infmap_1 = new Map();
      infresult.forEach(row => {
        infmap_1.set(row._id, row.cname);
      });

      let infMap_2 = new Map();
      infresult.forEach(row => {
        let name = row.classname.split('.');
        infMap_2.set(row._id, name[name.length-1])
      });
          
      let result = await this.Model.listWithPage(projectid, page, limit);
      let out=[];
            for(var i=0;i<result.length;i++){
              var tmp = {};
              tmp.id = result[i]._id;
              tmp.infcname = infmap_1.get(result[i].infid);
              tmp.infname = infMap_2.get(result[i].infid);
              tmp.cname = result[i].cname;
              tmp.onlinedate = result[i].onlinedate;
              tmp.ischl = result[i].ischl;
              tmp.mname = result[i].mname;
              if ( result[i].status === 0){
                tmp.status = '设计中';
              } else if ( result[i].status === 1 ){
                tmp.status = 'sit测试';
              } else if ( result[i].status === 2 ){
                tmp.status = 'uat测试';
              } else if ( result[i].status === 3 ){
                tmp.status = '已上线';
              }
              out.push(tmp);        
            }
            
       
      let count = await this.Model.listCount({ projectid });
  
      ctx.body = yapi.commons.resReturn({
        count: count,
        total: Math.ceil(count / limit),
        list: out
      });
    } catch (err) {
      ctx.body = yapi.commons.resReturn(null, 402, err.message);
    }
  }
  async listByInf(ctx) {
    let infid = ctx.request.query.infid;
    let page = ctx.request.query.page || 1,
        limit = ctx.request.query.limit || 10;

    if (!infid) {
      return (ctx.body = yapi.commons.resReturn(null, 400, 'infid不能为空'));
    }
    try {
      let infdata = await this.interfaceModel.get(infid);
      let dubboProject = await this.dubboProjectModel.getBaseInfo(infdata.projectid);
      if (dubboProject.projecttype === 'private') {
        if ((await this.checkAuth(dubboProject._id, 'dubboproject', 'view')) !== true) {
          return (ctx.body = yapi.commons.resReturn(null, 406, '没有权限'));
        }
      }

      let result = await this.Model.listByInfidWithPage(infid, page, limit);

      let count = await this.Model.listCount({ infid });

      ctx.body = yapi.commons.resReturn({
        count: count,
        total: Math.ceil(count / limit),
        list: result
      });
    } catch (err) {
      ctx.body = yapi.commons.resReturn(null, 402, err.message + '1');
    }
  }

  async addInf(ctx) {
    try {
      let params = ctx.request.body;
      params = yapi.commons.handleParams(params, {
        cname: 'string',
        projectid: 'number',
        desc: 'string'
      });

      if (!params.projectid) {
        return (ctx.body = yapi.commons.resReturn(null, 400, 'dubbo项目id不能为空'));
      }
      if (!this.$tokenAuth) {
        let auth = await this.checkAuth(params.projectid, 'dubboproject', 'edit');
        if (!auth) {
          return (ctx.body = yapi.commons.resReturn(null, 400, '没有权限'));
        }
      }

      if (!params.cname) {
        return (ctx.body = yapi.commons.resReturn(null, 400, '接口名称不能为空'));
      }

      let result = await this.interfaceModel.save({
        cname: params.cname,
        projectid: params.projectid,
        desc: params.desc,
        cuserid: this.getUid(),
        ctime: yapi.commons.time(),
        etime: yapi.commons.time()
      });

      let username = this.getUsername();
      yapi.commons.saveLog({
        content: `<a href="/user/profile/${this.getUid()}">${username}</a> 添加了接口  <a href="/dubboproject/${
            params.projectid
        }/interface/api/${result._id}">${params.cname}</a>`,
        type: 'dubboproject',
        uid: this.getUid(),
        username: username,
        typeid: params.projectid
      });

      ctx.body = yapi.commons.resReturn(result);
    } catch (e) {
      ctx.body = yapi.commons.resReturn(null, 402, e.message);
    }
  }

  async upInf(ctx) {
    try {
      let params = ctx.request.body;

      let username = this.getUsername();
      let infc = await this.interfaceModel.get(params.infid);

      let auth = await this.checkAuth(infc.projectid, 'dubboproject', 'edit');
      if (!auth) {
        return (ctx.body = yapi.commons.resReturn(null, 400, '没有权限'));
      }

      let result = await this.interfaceModel.up(params.infid, {
        cname: params.cname,
        desc: params.desc,
        etime: yapi.commons.time()
      });

      yapi.commons.saveLog({
        content: `<a href="/user/profile/${this.getUid()}">${username}</a> 更新了接口 <a href="/project/${
            infc.projectid
        }/interface/api/${params.infid}">${infc.cname}</a>`,
        type: 'dubboproject',
        uid: this.getUid(),
        username: username,
        typeid: infc.projectid
      });

      ctx.body = yapi.commons.resReturn(result);
    } catch (e) {
      ctx.body = yapi.commons.resReturn(null, 400, e.message);
    }
  }

  // 删除接口
  async del(ctx) {
    try {
      let id = ctx.request.body.id;

      if (!id) {
        return (ctx.body = yapi.commons.resReturn(null, 400, '方法id不能为空'));
      }

      let data = await this.Model.get(id);

      if (data.cuserid !== this.getUid()) {
        let auth = await this.checkAuth(data.projectid, 'dubboproject', 'danger');
        if (!auth) {
          return (ctx.body = yapi.commons.resReturn(null, 400, '没有权限'));
        }
      }

      // let inter = await this.Model.get(id);
      let result = await this.Model.del(id);

      // ???下面这两行代码有bug
      // yapi.emitHook('method_del', id).then();
      // await this.caseModel.delByMethodId(id);

      let username = this.getUsername();
      this.interfaceModel.get(data.infid).then(infc => {
        yapi.commons.saveLog({
          content: `<a href="/user/profile/${this.getUid()}">${username}</a> 删除了接口 <a href="/dubboproject/${
              infc.projectid
          }/interface/api/cat_${data.infid}">${infc.cname}</a> 下的方法 "${data.mname}"`,
          type: 'dubboproject',
          uid: this.getUid(),
          username: username,
          typeid: infc.projectid
        });
      });
      this.dubboProjectModel.up(data.projectid, { up_time: new Date().getTime() }).then();
      ctx.body = yapi.commons.resReturn(result);
    } catch (err) {
      ctx.body = yapi.commons.resReturn(null, 402, err.message);
    }
  }

  async delInf(ctx) {
    try {
      let id = ctx.request.body.infid;
      let infData = await this.interfaceModel.get(id);
      if (!infData) {
        ctx.body = yapi.commons.resReturn(null, 400, '不存在的接口');
      }

      if (infData.cuserid !== this.getUid()) {
        let auth = await this.checkAuth(infData.projectid, 'dubboproject', 'danger');
        if (!auth) {
          return (ctx.body = yapi.commons.resReturn(null, 400, '没有权限'));
        }
      }

      let username = this.getUsername();
      yapi.commons.saveLog({
        content: `<a href="/user/profile/${this.getUid()}">${username}</a> 删除了接口 "${
            infData.cname
        }" 及该接口下的方法`,
        type: 'dubboproject',
        uid: this.getUid(),
        username: username,
        typeid: infData.projectid
      });

      let methodData = await this.Model.listByInfId(id);

      for (const item of methodData) {
        try {
          yapi.emitHook('method_del', item._id).then();
          await this.caseModel.delByMethodId(item._id);
        } catch (e) {
          yapi.commons.log(e.message, 'error');
        }
      }
      await this.interfaceModel.del(id);
      let r = await this.Model.delByInfId(id);
      return (ctx.body = yapi.commons.resReturn(r));
    } catch (e) {
      yapi.commons.resReturn(null, 400, e.message);
    }
  }

  async upIndex(ctx) {
    try {
      let params = ctx.request.body;
      if (!params || !Array.isArray(params)) {
        ctx.body = yapi.commons.resReturn(null, 400, '请求参数必须是数组');
      }
      params.forEach(item => {
        if (item.id) {
          this.Model.upIndex(item.id, item.index).then(
              // res => {},
              err => {
                yapi.commons.log(err.message, 'error');
              }
          );
        }
      });

      return (ctx.body = yapi.commons.resReturn('成功！'));
    } catch (e) {
      ctx.body = yapi.commons.resReturn(null, 400, e.message);
    }
  }

  async upInfIndex(ctx) {
    try {
      let params = ctx.request.body;
      if (!params || !Array.isArray(params)) {
        ctx.body = yapi.commons.resReturn(null, 400, '请求参数必须是数组');
      }
      params.forEach(item => {
        if (item.id) {
          this.interfaceModel.upInfIndex(item.id, item.index).then(
              // res => {},
              err => {
                yapi.commons.log(err.message, 'error');
              }
          );
        }
      });

      return (ctx.body = yapi.commons.resReturn('成功！'));
    } catch (e) {
      ctx.body = yapi.commons.resReturn(null, 400, e.message);
    }
  }
  


  // 处理编辑冲突
  async solveConflict(ctx) {
    try {
      let id = parseInt(ctx.query.id, 10),
        result,
        userInst,
        userinfo,
        data;
      if (!id) {
        data = {
          param_err: 1,
          data: { message: 'id 参数有误'}
        }
        return ctx.websocket.send(JSON.stringify(data));
      }
      result = await this.Model.get(id);
      if (result == undefined) {
        data = {
          param_err: 2,
          data: { message: '接口方法不存在'}
        }
        return ctx.websocket.send(JSON.stringify(data));
      }
      if (result.euserid !== 0 && result.euserid !== this.getUid()) {
        userInst = yapi.getInst(userModel);
        userinfo = await userInst.findById(result.euserid);
        data = {
          errno: result.euserid,
          data: { uid: result.euserid, username: userinfo.username }
        };
      } else {
        this.Model.upEditUid(id, this.getUid()).then();
        data = {
          errno: 0,
          data: result
        };
      }
      ctx.websocket.send(JSON.stringify(data));
    } catch (err) {
      yapi.commons.log(err, 'error');
    }
  }


  /* TTC ADD */

  async addMethod(ctx){
    try {
      let params = ctx.request.body;
      params = yapi.commons.handleParams(params, {
        infid: 'number',
        cname: 'string',
        mname: 'string',
        inname: 'string',
        outname: 'string',
        status:'number',
        ischl:'number',
        projectid:'number'
      });

      // if (!params.projectid) {
      //   return (ctx.body = yapi.commons.resReturn(null, 400, '项目id不能为空'));
      // }
      // if (!this.$tokenAuth) {
      //   let auth = await this.checkAuth(params.projectid, 'project', 'edit');
      //   if (!auth) {
      //     return (ctx.body = yapi.commons.resReturn(null, 400, '没有权限'));
      //   }
      // }
      //
      // if (!params.cname) {
      //   return (ctx.body = yapi.commons.resReturn(null, 400, '名称不能为空'));
      // }

      let result = await this.Model.save({
        infid: params.infid,
        cname:params.cname,
        mname: params.mname,
        inname: params.inname,
        outname: params.outname,
        status:params.status,
        ischl:params.ischl,
        cuserid: this.getUid(),
        euserid:this.getUid(),
        ctime: yapi.commons.time(),
        etime: yapi.commons.time(),
        projectid:params.projectid
      });

      let inf = await this.interfaceModel.get(params.infid);
      let username = this.getUsername();
      let title = `<a href="/user/profile/${this.getUid()}">${username}</a> 为接口 <a href="/dubboproject/${
        params.projectid
      }/interface/api/${params.infid}">${inf.classname}</a> 添加了方法 <a href="/dubboproject/${
        params.projectid
      }/interface/dubbomethod/${result._id}">${params.mname}</a>`;

      yapi.commons.saveLog({
        content: title,
        type: 'dubboproject',
        uid: this.getUid(),
        username: username,
        typeid: params.projectid
      });

      ctx.body = yapi.commons.resReturn(result);
    } catch (e) {
      ctx.body = yapi.commons.resReturn(null, 402, e.message);
    }

  }

  /* TTC ADD ^^^^ */

  async schema2json(ctx) {
    let schema = ctx.request.body.schema;
    let res = {};
    for (var i = 0; i < schema.length; i++) {
      let name = schema[i].name;
      let field_type = schema[i].field_type;
      let length = parseInt(schema[i].length);
      if (field_type === 'String'){
        res[name] = Random.string('lower', length);
      } else if (field_type === 'Number'){
        let temp = Random.string('number', length);
        res[name] = Number(temp);
      } else if (field_type === 'List'){
        res[name] = [];
      } else if (field_type === 'Object'){
        res[name] = {};
      } else if (field_type === 'Boolean'){
        res[name] = Random.boolean();
      } else if (field_type === 'Integer'){
        let temp = Random.string('number', length);
        res[name] = parseInt(temp);
      } else {
        res[name] = null;
      }
    }
    return (ctx.body = res);
  }
}
module.exports = dubboMethodController;